package com.jyy.prefabricated.service.impl;

import com.jyy.prefabricated.dto.CourseCategoryExamDto;
import com.jyy.prefabricated.entity.CourseCategoryExam;
import com.jyy.prefabricated.mapper.CourseCategoryExamMapper;
import com.jyy.prefabricated.service.CourseCategoryExamService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 课程包分类考试表(CourseCategoryExam)表服务实现类
 *
 * @author mrl
 * @since 2021-10-14 19:53:19
 */
@Service("courseCategoryExamService")
public class CourseCategoryExamServiceImpl implements CourseCategoryExamService {
    @Resource
    private CourseCategoryExamMapper courseCategoryExamDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CourseCategoryExam queryById(Integer id) {
        return this.courseCategoryExamDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<CourseCategoryExam> queryAllByLimit(int offset, int limit) {
        return this.courseCategoryExamDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<CourseCategoryExam> queryAll(CourseCategoryExam courseCategoryExam) {
        return courseCategoryExamDao.queryAll(courseCategoryExam);
    }

    /**
     * 新增数据
     *
     * @param courseCategoryExam 实例对象
     * @return 实例对象
     */
    @Override
    public CourseCategoryExam insert(CourseCategoryExam courseCategoryExam) {
        this.courseCategoryExamDao.insert(courseCategoryExam);
        return courseCategoryExam;
    }

    @Override
    public int insertCourseCategoryExam(CourseCategoryExamDto courseCategoryExamDto) {
        int result = 0;
        courseCategoryExamDao.deleteByCcId(courseCategoryExamDto.getCourseCategoryId());
        if(courseCategoryExamDto.getCourseCategoryExams() != null && courseCategoryExamDto.getCourseCategoryExams().size() > 0){
            for (CourseCategoryExam courseCategoryExam:courseCategoryExamDto.getCourseCategoryExams()) {
                courseCategoryExam.setCcId(courseCategoryExamDto.getCourseCategoryId());
                courseCategoryExam.setCreateTime(new Date());
            }
            result = courseCategoryExamDao.insertBatch(courseCategoryExamDto.getCourseCategoryExams());
        }
        return result;
    }

    /**
     * 修改数据
     *
     * @param courseCategoryExam 实例对象
     * @return 实例对象
     */
    @Override
    public CourseCategoryExam update(CourseCategoryExam courseCategoryExam) {
        this.courseCategoryExamDao.update(courseCategoryExam);
        return this.queryById(courseCategoryExam.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.courseCategoryExamDao.deleteById(id) > 0;
    }
}